package com.dc.ftp.test;

import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import com.dc.ftp.R;
import com.dc.ftp.base.SPBaseActivity;
import com.dc.ftp.test.Fragment.ImageFragment;
import com.dc.ftp.test.Fragment.TextFragment;


public class FragmentTestActivity extends SPBaseActivity {
    TextFragment textFragment;
    ImageFragment imgFragment;
    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.test_fragmenttest);

        textFragment = new TextFragment();
        imgFragment = new ImageFragment();

    }
    public void add(View v){
        //创建事务
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        //操作指令
        ft.add(R.id.fl_content,textFragment);
        //提交事务
        ft.commit();
    }
    public void remove(View v){
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        //从Activity中移除一个Fragment，如果被移除的Fragment没有添加到回退栈（回退栈后面会详细说），这个Fragment实例将会被销毁。
        ft.remove(textFragment);
        ft.commit();
    }

    public void replace(View v){
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        //使用另一个Fragment替换当前的，实际上就是remove()然后add()的合体
        ft.replace(R.id.fl_content,imgFragment);
        ft.commit();
    }

    public void attach(View v){
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        //重建视图，附件到UI上并显示
        ft.attach(textFragment);
        ft.commit();
    }
    public void detach(View v){
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        //会将view从UI中移除,和remove()不同,此时fragment的状态依然由FragmentManager维护
        ft.detach(textFragment);
        ft.commit();
    }
    public void show(View v){
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        //显示之前隐藏的Fragment
        ft.show(textFragment);
        ft.commit();
    }
    public void hide(View v){
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        //当你的fragment数量固定很少时隐藏当前的Fragment，仅仅是设为不可见，并不会销毁
        ft.hide(textFragment);
        ft.commit();
    }
    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void initView() {

    }

    @Override
    public void initData() {

    }

    @Override
    public void initEvent() {

    }

    // 捕获返回键的方法1
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            // 按下BACK，同时没有重复
            Toast.makeText(this, "按下BACK，同时没有重复onKeyDown()",Toast.LENGTH_LONG).show();

        }
          finish();
        return true;
    }

    // 捕获返回键的方法2
    @Override
    public void onBackPressed() {
       Toast.makeText(this, "按后退键onBackPressed()",Toast.LENGTH_LONG).show();

        super.onBackPressed();
        finish();

    }
}



